package com.tengju.bff.interfaces.covercharge.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.idanchuang.component.base.JsonResult;
import com.tengju.bff.interfaces.covercharge.constant.CooperationEnum;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.constant.LetaxWithdrawStatusEnum;
import com.tengju.bff.interfaces.covercharge.entity.vo.ServiceLetaxWithdrawLogVo;
import com.tengju.bff.interfaces.covercharge.entity.vo.WithdrawLogRecordVo;
import com.tengju.bff.interfaces.covercharge.service.IServiceLetaxWithdrawLogService;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.user.application.user.UserDataAuthService;
import com.tengju.user.domain.model.user.Staff;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author xx
 * 提现记录查询
 */

@Api(value = "withdrawalRecord", tags = "Admin提现记录查询")
@RestController
@Slf4j
@RequestMapping("/manage/bill/withdrawalRecord")
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class AdminWithdrawalRecordController {

    private final IServiceLetaxWithdrawLogService serviceLetaxWithdrawLogService;
    private final UserDataAuthService userDataAuthService;

    @GetMapping
    @ApiOperation(value = "提现记录查询", notes = "提现记录查询")
    @RequiresRoles("sso")
    public ApiResponse<IPage<WithdrawLogRecordVo>> getRegistration(String codeId, String status,
                                                                   @RequestParam(defaultValue = "0", name = "pageNo") Integer pageNo,
                                                                   @RequestParam(defaultValue = "10", name = "limit") Integer limit) {

        Page<WithdrawLogRecordVo> pages = serviceLetaxWithdrawLogService.queryPageByIdCode(new Page<>(pageNo, limit), codeId, status);
        List<WithdrawLogRecordVo> list = pages.getRecords();
        for (WithdrawLogRecordVo withdrawLogRecordVo : list) {
            if (StringUtils.isNotBlank(withdrawLogRecordVo.getRegisteredInfo())) {
                try {
                    JSONObject info = new JSONObject();
                    if (withdrawLogRecordVo.getAuthType().equals(CooperationEnum.PERSONAL.getCode())) {
                        info = JSON.parseObject(withdrawLogRecordVo.getRegisteredInfo());
                    }
                    if (withdrawLogRecordVo.getAuthType().equals(CooperationEnum.REGISTSOHO.getCode())) {
                        info = JSON.parseArray(withdrawLogRecordVo.getRegisteredInfo()).getJSONObject(0);
                    }
                    withdrawLogRecordVo.setBankName(StringUtils.isNotBlank(info.getString("bank_name")) ? info.getString("bank_name") : "无");
                    withdrawLogRecordVo.setIdcardName(StringUtils.isNotBlank(info.getString("idcard_name")) ? info.getString("idcard_name") : "无");
                    withdrawLogRecordVo.setIdcardNo(StringUtils.isNotBlank(info.getString("idcard_no")) ? info.getString("idcard_no") : "无");
                } catch (Exception e) {
                    log.error("解析该用户信息json失败,用户信息：{},{}", withdrawLogRecordVo.getRegisteredInfo(), e.getMessage());
                }
            }
            int state = withdrawLogRecordVo.getStatus();
            if (LetaxWithdrawStatusEnum.APPLY.getCode() == state || LetaxWithdrawStatusEnum.REJECT.getCode() == state || LetaxWithdrawStatusEnum.PASS.getCode() == state || LetaxWithdrawStatusEnum.CANCEL.getCode() == state) {
                //这几种状态下不显示打款时间
                withdrawLogRecordVo.setWithdrawTime("");
            }
        }
        Staff staff = ManageUtil.getOperateUser();
        pages.setRecords(userDataAuthService.dataMasking(pages.getRecords(), staff.getStaffId()));
        return ApiResponse.newSuccess(pages);
    }

    @PostMapping("/getShortFallData")
    @ApiOperation(value = "异常提现记录查询", notes = "异常提现记录查询")
    public ApiResponse<IPage<ServiceLetaxWithdrawLogVo>> getShortFallData(@RequestBody ServiceLetaxWithdrawLogVo serviceLetaxWithdrawLogVo) {
        if (serviceLetaxWithdrawLogVo.getCycleAddId() == null ) {
            return ApiResponse.newFailure(CoverChargeErrorCode.PARAM_ERROR.getCode(), "参数缺失");
        }
        Page page = new Page(serviceLetaxWithdrawLogVo.getPageNo(), serviceLetaxWithdrawLogVo.getLimit());
        return ApiResponse.newSuccess(serviceLetaxWithdrawLogService.queryPageByCycleAddId(page, serviceLetaxWithdrawLogVo.getCycleAddId()));
    }
}
